package ltd.itlover.yqblog.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.github.ybq.android.spinkit.SpinKitView;

import org.json.JSONObject;

import javax.inject.Inject;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import ltd.itlover.yqblog.R;
import ltd.itlover.yqblog.application.MyApplication;
import ltd.itlover.yqblog.dto.Result;
import ltd.itlover.yqblog.entity.User;
import ltd.itlover.yqblog.network.AccountApiService;
import retrofit2.HttpException;

@AndroidEntryPoint
public class RegisterActivity extends AppCompatActivity {
    private TextView textEmail;
    private TextView textPassword;
    private TextView textUsername;
    private Button btnRegister;
    private SpinKitView loadingAnimate;

    @Inject
    AccountApiService accountApiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();


    }

    private void init(){
        textEmail = (TextView) findViewById(R.id.register_input_email);
        textPassword = (TextView) findViewById(R.id.register_input_password);
        textUsername = (TextView) findViewById(R.id.register_input_username);
        btnRegister = (Button) findViewById(R.id.btn_register);
        loadingAnimate = (SpinKitView) findViewById(R.id.spin_kit);
        loadingAnimate.setVisibility(View.INVISIBLE);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadingAnimate.setVisibility(View.VISIBLE);
                User user = checkForm();
                if(user == null){
                   //用户输入不合法则不进行提交
                    makeInfo("输入信息完整");
                }
                else{
                    requestRegister(user);
                }

                //输入合法则提交表单

            }
        });
    }

    private void requestRegister(User user){
        setStatusToLoading();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        accountApiService.register(user)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                    @Override
                    public void accept(Result<String> userResult) throws Exception {
                        makeInfo("注册成功");
                        finish();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(throwable instanceof HttpException){
                            HttpException httpException = (HttpException) throwable;
                            String s = httpException.response().errorBody().string();
                            JSONObject jsonObject = new JSONObject(s);
                            makeInfo(jsonObject.getString("msg"));
                        }
                        else{
                            makeInfo("网络异常");
                        }
                    }
                });
        releaseLoading();
    }

    private User checkForm(){
        String email =  textEmail.getText().toString();
        String password = textPassword.getText().toString();
        String username = textUsername.getText().toString().trim();
        if(StrUtil.hasBlank(username) || StrUtil.hasBlank(password) || StrUtil.hasBlank(email)){

            return null;
        }
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        return user;
    }

    private void setStatusToLoading(){
        btnRegister.setText("正在注册...");
//        btnRegister.setEnabled(false);
//        textUsername.setEnabled(false);
//        textPassword.setEnabled(false);
//        textEmail.setEnabled(false);
        loadingAnimate.setVisibility(View.VISIBLE);
    }

    private void releaseLoading(){
        btnRegister.setText("确认注册");
//        btnRegister.setEnabled(true);
//        textUsername.setEnabled(true);
//        textPassword.setEnabled(true);
//        textEmail.setEnabled(true);
        loadingAnimate.setVisibility(View.INVISIBLE);
    }
    private void makeInfo(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}